define(['./module'],function(paymentmodule){
	'use strict';

	/*=====End Of Page Element Directive=====*/
	
	/*=====Begin Of Save and Edit PaymentM=====*/
	paymentmodule.controller('payment',ServerSavePayment);
	function ServerSavePayment($scope,$rootScope,$http,$location,Upload,$timeout){
		$scope.gtparams=[];
		$scope.index=1;
		
		$scope.$watch('files',function(){
			$scope.upload($scope.files);
		});
		$scope.allupimgs=[];
		$scope.upload=function(files){
			if(files&&files.length){
				for(var i=0;i<files.length;i++){
					var file=files[i];
					var filename=files[i].name;
					if(!file.$error){
						Upload.upload({
							url:'../mall/files/ajaxFileUploadsImgs.action',
							data:{
								file:file,
								'filename':filename
							}
						}).progress(function (evt) {
		                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
		                    $scope.progress=progressPercentage;
		                }).success(function (data, status, headers, config) {
		                    $timeout(function() {
		                    	if(data.sucflag){
		                    		$scope.allupimgs.push(data);
		                    	}
		                    });
		                    
		                });
					}
				}
			}
		}
		
		$scope.remove=function(index){
			$scope.allupimgs.splice(index,1);
		}
		
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){
			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加支付支付方式信息&参数';
			$scope.status="1";
			$scope.paymentInterface='1';
			$scope.isFast='0';
			//保存数据方法
			$scope.save=function(){
				if(validate()){
					var account=$scope.account;
					var des=$scope.des;
					if(des==undefined){
						des="";
					}
					var isFast=$scope.isFast;
					var partnerid=$scope.partnerid;
					var paymentCode=$scope.paymentCode;
					var paymentFree=$scope.paymentFree;
					if(paymentFree==undefined){
						paymentFree="";
					}
					var paymentInterface=$scope.paymentInterface;
					if(paymentInterface==undefined){
						paymentInterface="";
					}
					var paymentname=$scope.paymentname;
					var safecode=$scope.safecode;
					if(safecode==undefined){
						safecode="";
					}
					var logo=angular.toJson($scope.allupimgs);
					var status=$scope.status;
					$http({
						method:'POST',
						url:'../mall/payment/save.action',
						data:{
							'account':account,
							'des':des,
							'isFast':isFast,
							'partnerid':partnerid,
							'paymentCode':paymentCode,
							'paymentFree':paymentFree,
							'paymentInterface':paymentInterface,
							'paymentname':paymentname,
							'safecode':safecode,
							'logo':logo,
							'status':status
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
									show:true,
									msg:'支付方式参数添加成功'
							}
							$location.path('/paymentment');
//							$scope.errors={
//									show:false
//							}
						} else {
							$scope.errors={
									show:true,
									msg:data.msg
							}
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
								show:true,
								msg:'系统异常'
						}
					});
				}
			}
		}
		
		//如果operate是edit则执行保存行为
		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/payment/find.action',
					data:{
						'paymentid':id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.title='正在对支付方式'+ data.bean.paymentname +'进行编辑';
						$scope.account=data.bean.account;
						$scope.des=data.bean.des;
						$scope.isFast=data.bean.isFast;
						$scope.partnerid=data.bean.partnerid;
						$scope.paymentCode=data.bean.paymentCode;
						$scope.paymentFree=data.bean.paymentFree;
						$scope.paymentInterface=data.bean.paymentInterface;
						$scope.paymentname=data.bean.paymentname;
						$scope.safecode=data.bean.safecode;
						$scope.allupimgs.push(data.ifb);
						$scope.status=data.bean.status;
						$scope.updatebtn={show:true};
						if(data.bean.paymentCode=="WYZF" || data.bean.paymentname=="网银支付"){
							$scope.nets={show:true};
						};
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
				//更新数据方法
				$scope.update=function(){
					if(validate()){
						var account=$scope.account;
						var des=$scope.des;
						if(des==undefined){
							des="";
						}
						var isFast=$scope.isFast;
						var partnerid=$scope.partnerid;
						var paymentCode=$scope.paymentCode;
						var paymentFree=$scope.paymentFree;
						if(paymentFree==undefined){
							paymentFree="";
						}
						var paymentInterface=$scope.paymentInterface;
						if(paymentInterface==undefined){
							paymentInterface="";
						}
						var paymentname=$scope.paymentname;
						var safecode=$scope.safecode;
						if(safecode==undefined){
							safecode="";
						}
						var logo=angular.toJson($scope.allupimgs);
						var status=$scope.status;
						$http({
							method:'POST',
							url:'../mall/payment/update.action',
							data:{
								'account':account,
								'des':des,
								'isFast':isFast,
								'partnerid':partnerid,
								'paymentCode':paymentCode,
								'paymentFree':paymentFree,
								'paymentInterface':paymentInterface,
								'paymentname':paymentname,
								'safecode':safecode,
								'logo':logo,
								'status':status,
								'paymentid':id
							}
						}).
						success(function(data,status,headers,config){
							if(data.sucflag){
								$scope.info={
										show:true,
										msg:'支付方式参数更新成功'
								}
								$location.path('/paymentment');
							} else {
								$scope.errors={
									show:true,
									msg:data.msg
								}
							}
						}).
						error(function(data,status,headers,config){
							$scope.errors={
									show:true,
									msg:'系统异常'
							}
						});
					}
				}
				
			}
		}
		
		$scope.netsilver=function(){
			var id=$location.search().id;
			$location.path('/netsilverment').search({'operate':'find','paymentid':id});
		}
		
		
		//表单字段验证
		function validate(){
			var paymentname=$scope.paymentname;
			if(paymentname==undefined||paymentname==""){
				$scope.errors={
						show:true,
						msg:"请输入支付名称"
				}
				return false;
			}
			var paymentCode=$scope.paymentCode;
			if(paymentCode==undefined||paymentCode==""){
				$scope.errors={
						show:true,
						msg:"请输入支付代码"
				}
				return false;
			}
			var account=$scope.account;
			if(account==undefined||account==""){
				$scope.errors={
						show:true,
						msg:"请输入支付账号"
				}
				return false;
			}
			var partnerid=$scope.partnerid;
			if(partnerid==undefined||partnerid==""){
				$scope.errors={
						show:true,
						msg:"请输入支付合作者身份ID"
				}
				return false;
			}
			return true;
		}
	}
	
	/*=====End Of Save and Edit PaymentM=====*/
	
	/*=====Begin Of Find List PaymentM=====*/
	paymentmodule.controller('paymentlist',ServerPaymentListCtrl);
	//查询列表数据
	function ServerPaymentListCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/payment/findByPage.action',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')
		
		$scope.$on('handleRequest',function(){
			
		});
		
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			          DTColumnBuilder.newColumn('paymentname').withTitle('名称').notSortable(),
			          DTColumnBuilder.newColumn('paymentCode').withTitle('代码').notSortable(),
			          DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="edit('+data.id+')"><i class="fa fa-edit"></i></button>';
		}
		
		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}
		
		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/payment').search({'operate':'edit','id':id});
		}
		
		/**
		 * 跳转到文章信息页面
		 */
		$scope.save=function(){
			$location.path('/payment').search({'operate':'save'});
		}
		
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/payment/del.action',
					data:{
						'ids':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'批量删除成功'
						}
						reloadData();
					}
					
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}
		
	}
});